package com.example.demo.application.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.application.common.MyConstants;
import com.example.demo.application.common.MyJsonUtils;
import com.example.demo.application.common.MyConstantsRespone;
import com.example.demo.application.model.RemindEntity;
import com.example.demo.application.myredis.RedisHelper;
import com.example.demo.application.service.IMsgService;
import com.example.demo.application.model.LoginUserEntity;
import com.example.demo.application.model.MindMode;
import com.example.demo.application.model.MsgEntity;
import com.example.demo.application.model.NodeEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "笔记")
@RestController
@EnableAutoConfiguration
//@RequestMapping(path="/") 
public class RemindCountController {

	protected final Logger log = LoggerFactory.getLogger(RemindCountController.class);

	@Resource(name = "remideService")
	private IMsgService remideService;

	@ApiOperation(value = "获得笔记总数", notes = "获得笔记总数")
	@RequestMapping(path = "/remide/getnodenumber", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap getNodeNumber(HttpServletRequest request, @RequestBody LoginUserEntity form) throws Exception {
		ModelMap modelMap = new ModelMap();

		RemindEntity bar = remideService.findRemindByUser(form.getToken());
		modelMap.put("userInfo", bar);
		modelMap.put("rest", MyConstantsRespone.success);
		return modelMap;
	}

	@ApiOperation(value = "获得未读消息数、提示数", notes = "获得未读消息数")
	@RequestMapping(path = "/remide/getRemind", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap getRemind(HttpServletRequest request, @RequestBody LoginUserEntity form) throws Exception {
		ModelMap modelMap = new ModelMap();

		RemindEntity bar = remideService.findRemindByUser(form.getToken());
		MindMode mm = MyJsonUtils.parse(bar.getRemind_json(), MindMode.class);
		modelMap.put("Remind", mm);
		modelMap.put("rest", MyConstantsRespone.success);
		return modelMap;
	}

	@ApiOperation(value = "获得首页笔记", notes = "获得首页笔记")
	@RequestMapping(path = "/remide/getfirstpage", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap getFirstPage(HttpServletRequest request, @RequestBody LoginUserEntity form) throws Exception {
		ModelMap modelMap = new ModelMap();

		List<NodeEntity> nodes = remideService.findNodeByUser(form.getToken());
		modelMap.put("nodes", nodes);
		modelMap.put("rest", MyConstantsRespone.success);
		return modelMap;
	}

	@ApiOperation(value = "新增一条首页笔记", notes = "新增一条首页笔记")
	@RequestMapping(path = "/remide/addFirstPageNode", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap addFirstPageNode(HttpServletRequest request, @RequestBody NodeEntity form) throws Exception {
		ModelMap modelMap = new ModelMap();
		String userToken = request.getHeader(MyConstants.TOKEN_AUTH);
		if (null != userToken) {
			List<LoginUserEntity> users =  (List<LoginUserEntity>) RedisHelper.getAllObject(MyConstants.LOGIN_USER + userToken);
			LoginUserEntity user  = users.get(0);
			if (null != user) {
				form.setUesr_id(user.getId());
			}
			remideService.addNodeByUser(form);
			modelMap.put("rest", MyConstantsRespone.success);
		}else {
			modelMap.put("rest", MyConstantsRespone.fail);
		}
		return modelMap;
	}

	@ApiOperation(value = "获得第二页消息", notes = "获得第二页消息")
	@RequestMapping(path = "/remide/getsecondppage", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap getSecondPage(HttpServletRequest request, @RequestBody LoginUserEntity form) throws Exception {
		ModelMap modelMap = new ModelMap();

		List<MsgEntity> msgs = remideService.findMsgByUser(form.getToken());
		modelMap.put("messageInfo", msgs);
		modelMap.put("rest", MyConstantsRespone.success);
		return modelMap;
	}

	@ApiOperation(value = "新增一条第二页消息", notes = "新增一条第二页消息")
	@RequestMapping(path = "/remide/addSecondPageMsg", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap addSecondPageMsg(HttpServletRequest request, @RequestBody MsgEntity form) throws Exception {
		ModelMap modelMap = new ModelMap();
		String userToken = request.getHeader(MyConstants.TOKEN_AUTH);
		if (null != userToken) {
			LoginUserEntity user = (LoginUserEntity) RedisHelper.getObject(MyConstants.LOGIN_USER, userToken);
			if (null != user) {
				form.setUesr_id(user.getId());
			}
			remideService.addMsgByUser(form);
			modelMap.put("rest", MyConstantsRespone.success);
		}else {
			modelMap.put("rest", MyConstantsRespone.fail);
		}
		return modelMap;
	}

	@ApiOperation(value = "获得第三页", notes = "获得第三页")
	@RequestMapping(path = "/remide/getthreeppage", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap getThreePage(@RequestBody LoginUserEntity form) throws Exception {
		ModelMap modelMap = new ModelMap();

		RemindEntity bar = remideService.findRemindByUser(form.getToken());
		modelMap.put("pictrueInfo", bar);
		modelMap.put("rest", MyConstantsRespone.success);
		return modelMap;
	}

	@ApiOperation(value = "获得第四页", notes = "获得第四页")
	@RequestMapping(path = "/remide/getthirdppage", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap getThirdPage(@RequestBody LoginUserEntity form) throws Exception {
		ModelMap modelMap = new ModelMap();

		RemindEntity bar = remideService.findRemindByUser(form.getToken());
		modelMap.put("userInfo", bar);
		modelMap.put("rest", MyConstantsRespone.success);
		return modelMap;
	}
}
